package com.ocom.xfrecode.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ocom.common.entity.card.EntityXfRecord;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.xfrecde.NotMealRecodeRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.entity.card.EntityNotMealRecode;
import com.ocom.xfrecode.service.NotMealRecodeService;
import com.ocom.xfrecode.service.XfRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ocom.common.constant.RecodeUrl;

@Slf4j
@RestController
public class NotMealRecodeController {

    @Autowired
    private NotMealRecodeService mealRecodeService;

    @Autowired
    private XfRecordService xfRecordService;
    @PostMapping(RecodeUrl.URL_saveNotMealRecode)
    public Result saveNotMealRecode(@RequestBody EntityNotMealRecode entityNotMealRecode){
        try {
            entityNotMealRecode.setMonthValue(DateUtil.format(entityNotMealRecode.getMealDate(),"yyyyMM"));
            EntityXfRecord xfRecord = Convert.convert(EntityXfRecord.class, entityNotMealRecode);
            EntityXfRecord xfRecordA = this.xfRecordService.getOneRecode(xfRecord);
            if(ObjectUtil.isNotEmpty(xfRecordA)){
                log.info("有消费记录，不记录");
                return ResultUtil.success();
            }
            boolean result = this.mealRecodeService.save(entityNotMealRecode);
            if(result){
                log.info("记录数据");
                return ResultUtil.success();
            }
        }catch (Exception e){
            log.error("记录错误：{}",e.getMessage());
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.Err_3043);
    }

    @PostMapping(RecodeUrl.getNotMealRecode)
    public Result getNotMealRecode(@RequestBody NotMealRecodeRequest request){
        return ResultUtil.success(this.mealRecodeService.getNotMealRecode(request));
    }
}
